#!/bin/bash

# -e ：判断文件或目录是否存在
# -d ：判断是不是目录以及是否存在
# -f ：判断是不是普通文件以及是否存在
# -r ：判断是否有读权限
# -w ：判断是否有写权限
# -x ：判断是否有执行权限

# 字体颜色
# echo -e "\e[30m 黑色 \e[0m"
# echo -e "\e[31m 红色 \e[0m"
# echo -e "\e[32m 绿色 \e[0m"
# echo -e "\e[33m 黄色 \e[0m"
# echo -e "\e[34m 蓝色 \e[0m"
# echo -e "\e[35m 紫色 \e[0m"
# echo -e "\e[36m 青色 \e[0m"
# echo -e "\e[37m 白色 \e[0m"

# info_red(){
#   echo -e "\e[31m $@ \e[0m"
# }
# info_green(){
#   echo -e "\e[32m $@ \e[0m"
# }
# info_yellow(){
#   echo -e "\e[33m $@ \e[0m"
# }
# info_blue(){
#   echo -e "\e[34m $@ \e[0m"
#   # printf "\e[34m $@\n\e[0m"
# }
# info_pink(){
#   echo -e "\e[35m $@ \e[0m"
# }
# info_sky(){
#   echo -e "\e[36m $@ \e[0m"
# }


# command_exists() {
#   command -v "$@" >/dev/null 2>&1
# }


## ----------------------------git settings-------------------------------------

insecure_key=$(cat /home/vagrant/.ssh/authorized_keys)
echo "setup ssh"
if [ -f ~/.ssh/github_id_rsa ]; then 
  echo "ssh has been setup!!!"
else 
  echo "setup ssh for $lsb_dist now!!!"
  export GIT_SSL_NO_VERIFY=1
  rm -rf ~/.ssh
  rm -rf ~/git_settings
  cd ~
  git clone https://gitee.com/lgf1244/git_settings.git
  cd ~/git_settings/.ssh
  # chmod 600 *_id_rsa
  # chmod 600 config
  cp -rp ~/git_settings/.ssh ~
  rm -rf rp ~/git_settings
  # cd ~
  # git clone git@gitee.com:lgf1244/ssh.git
  # rm -rf ~/.ssh 
  # mv ~/ssh ~/.ssh
  cd ~/.ssh
  echo $insecure_key >> authorized_keys
  chmod 600 config authorized_keys *_rsa
  chmod 644 *.pub
fi

## ----------------------------pip settings-------------------------------------

if command -v "pip" >/dev/null 2>&1; then
  pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
  pip --version
else 
  echo "no pip command found, please install it(python3-pip) first!!!"
fi


## ----------------------------gem settings-------------------------------------

if command -v "gem" >/dev/null 2>&1; then
  gem sources --add  https://gems.ruby-china.com/ --remove https://rubygems.org/
  gem sources -l
  ruby -v
  gem -v
else 
  echo "no gem command found, please install it(ruby) first!!!"
fi

## ----------------------------npm settings-------------------------------------
if (command -v "npm" >/dev/null 2>&1) && [ `whoami` = 'root' ];then
  echo "npm settings"
  npm i -g n
  n stable
  node -v
  npm -v
else 
  echo "no gem command found, please install it(npm) first!!!"
fi


## ----------------------------cargo settings-------------------------------------

# [source.crates-io]
# registry = "https://github.com/rust-lang/crates.io-index"
# # 指定镜像
# replace-with = '镜像源名' # 如：tuna、sjtu、ustc，或者 rustcc

# # 注：以下源配置一个即可，无需全部
# # 中国科学技术大学
# [source.ustc]
# registry = "git://mirrors.ustc.edu.cn/crates.io-index"
# # 上海交通大学
# [source.sjtu]
# registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
# # 清华大学
# [source.tuna]
# registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
# # rustcc社区
# [source.rustcc]
# registry = "https://code.aliyun.com/rustcc/crates.io-index.git"


if command -v "cargo" >/dev/null 2>&1; then
  echo "cargo settings"
  mkdir -p ~/.cargo
  echo "[source.crates-io]" > ~/.cargo/config
  echo 'registry = "https://github.com/rust-lang/crates.io-index"' >> ~/.cargo/config
  echo 'replace-with = "ustc"' >> ~/.cargo/config
  echo "[source.ustc]" >> ~/.cargo/config
  echo 'registry = "git://mirrors.ustc.edu.cn/crates.io-index"' >> ~/.cargo/config
  cargo install lsd

else 
  echo "no gem command found, please install it(cargo) first!!!"
fi

## ----------------------------golang settings-------------------------------------

if command -v "go" >/dev/null 2>&1; then
  echo "golang settings"
else 
  echo "no gem command found, please install it(go) first!!!"
fi

# ------------------------install oh-my-zsh------------------------

# git clone https://github.com/ohmyzsh/ohmyzsh.git && ./ohmyzsh/tools/install.sh && \
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting && \
# git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions && \
# git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k && \
# git clone https://github.com/paulirish/git-open.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/git-open && \
# sed -i 's/^plugins=(/plugins=(docker docker-compose python npm autojump zsh-autosuggestions zsh-syntax-highlighting z /' ~/.zshrc && \
# sed -i 's/^ZSH_THEME="robbyrussell"/ZSH_THEME="juanghurtado"/' ~/.zshrc

echo "install oh-my-zsh now!!!"
if [ -d ~/.oh-my-zsh ]; then 
  echo "already installed oh-my-zsh, will skip it"
else
  rm -rf ./ohmyzsh
  git clone https://gitee.com/studyzy/ohmyzsh.git
  ZSH= sh ./ohmyzsh/tools/install.sh
  git clone https://gitee.com/Kurt-Code/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  git clone https://gitee.com/Kurt-Code/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
  git clone https://gitee.com/yanzhongqian/git-open.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/git-open
  sed -i 's/^plugins=(/plugins=(docker docker-compose python npm zsh-autosuggestions zsh-syntax-highlighting z /' ~/.zshrc
  sed -i 's/^ZSH_THEME="robbyrussell"/ZSH_THEME="juanghurtado"/' ~/.zshrc
  rm -rf ./ohmyzsh
  echo finished install oh-my-zsh
fi


# ------------------------install oh-my-bash------------------------
# git clone https://github.com/ohmybash/oh-my-bash.git && bash ./oh-my-bash/tools/install.sh
echo "install oh-my-bash now!!!"
if [ -d ~/.oh-my-bash ]; then 
  echo "already installed oh-my-bash, will skip it"
else
  rm -rf ./oh-my-bash
  git clone https://gitee.com/oh-my-shell/oh-my-bash.git
  bash ./oh-my-bash/tools/install.sh
  rm -rf ./oh-my-bash
  echo finished install oh-my-bash
fi

# ------------------------install oh-my-fish------------------------
# support os: ubuntu-16.04+ debian10+
# git clone https://github.com/oh-my-fish/oh-my-fish.git  && ./oh-my-fish/bin/install --offline --noninteractive
# if [ -x /usr/bin/fish ]; then
#   info_green "install oh-my-fish now!!!"
#   if [ -d ~/.local/share/omf ]; then 
#     info_red "already installed oh-my-fish, will skip it"
#   else
#     rm -rf ./oh-my-fish
#     git clone https://gitee.com/lgf1244/oh-my-fish.git
#     ./oh-my-fish/bin/install --offline --noninteractive
#     rm -rf ./oh-my-fish
#     echo finished install oh-my-fish
#   fi
# else 
#   info_red "not install fish on first"
# fi

# ------------------------install oh-my-tmux------------------------

# $ cd
# $ git clone https://github.com/gpakosz/.tmux.git
# $ ln -s -f .tmux/.tmux.conf
# $ cp .tmux/.tmux.conf.local .


echo "install oh-my-tmux now!!!"
if [ -d ~/.tmux ]; then 
  echo "already installed oh-my-tmux, will skip it"
else
  git clone https://gitee.com/lgf1244/tmux.git ~/.tmux
  ln -s -f ~/.tmux/.tmux.conf ~/.tmux.conf
  cp ~/.tmux/.tmux.conf.local ~
  echo finished install oh-my-tmux
fi


# ------------------------install oh-my-posh------------------------
echo "install oh-my-posh now!!!"
if [ -d ~/.oh-my-posh ]; then 
  echo "already installed oh-my-posh, will skip it"
else
  echo finished install oh-my-posh
fi

#  ------------------------install cheat.sh---------------------------
if [ -x /usr/local/bin/cht.sh ]; then
  echo "cheat.sh has been installed"
  # cht.sh js parse json
  # curl cht.sh/js/parse+json
else
  curl -s https://cht.sh/:cht.sh | tee /usr/local/bin/cht.sh && chmod +x /usr/local/bin/cht.sh
fi 

# ------------------------install vim-fast---------------------------
# supprt os: Ubuntu-20.04+ Debian-10+
# WARNING:vim is too old,so it can support some feature!  支持8.0以上的版本
# https://gitee.com/mirrorvim/vim-fast
# if [ -f ~/vimrc-no-plug ]; then 

if command -v "vim" >/dev/null 2>&1; then
  vim_ver=$(vim --version | grep "VIM - Vi IMproved" | sed  "s/VIM - Vi IMproved//g" | grep -o "[0-9]\.[0-9]")
  if [ `expr $vim_ver \> 8.0` -eq 1 ];then
    #  echo "vim_version is bigger than 8.0, "
    if [ -L  ~/.vimrc ] && [ $(readlink ~/.vimrc) == ~/vimrc-no-plug ]; then
      echo "fast-vim has been setup"
    else
      echo "setup fast-vim has now!!!"
      # curl https://gitee.com/mirrorvim/vim-fast/raw/master/shell/websimple.sh | bash
      cd ~
      rm -f ~/.vimrc
      curl -o vimrc-no-plug https://gitee.com/mirrorvim/vim-fast/raw/master/vimrc-no-plug
      ln -s -f ~/vimrc-no-plug ~/.vimrc
    fi
  else
      echo "vim_version is less than 8.0, vim-fast only support 8.0+, please upgrade vim firstly!!!"
  fi
else
  echo "please install vim first!!!"
fi


## ----------------------------dotfiles settings-------------------------------------

# if [ -d ~/.dotfiles ]; then 
#   echo "dotfiles has been download before, please update it now!!!"
#   cd ~/.dotfiles && git pull && cd ~
# else 
#   echo "will downloas dotfiles now!!!"
#   git clone git@gitee.com:lgf1244/dotfiles.git ~/.dotfiles
# fi

# # ls -l ~/codes | awk -F'->' '{print $2}' | tr -d ' '
# # readlink ~/codes

# if [ -L  ~/.zshrc ] && [ $(readlink ~/.zshrc) == ~/.dotfiles/.zshrc ]; then
#   echo "zshrc softlink has been setup!!!"
# else 
#   echo "setup zshrc softlink now!!!"
#   # mv ~/.zshrc ~/.zshrc.bak
#   rm -f ~/.zshrc
#   ln -s -f ~/.dotfiles/.zshrc  ~/.zshrc
# fi

# if [ -L  ~/.bashrc ] && [ $(readlink ~/.bashrc) == ~/.dotfiles/.bashrc ]; then
#   echo "bashrc softlink has been setup!!!"
# else 
#   echo "setup bashrc softlink now!!!"
#   # mv ~/.bashrc ~/.bashrc.bak
#   rm -f ~/.bashrc
#   ln -s -f ~/.dotfiles/.bashrc  ~/.bashrc
# fi

# if [ -L  ~/.gitconfig ] && [ $(readlink ~/.gitconfig) == ~/.dotfiles/.gitconfig ]; then
#   echo "gitconfig softlink has been setup!!!"
# else 
#   echo "setup gitconfig softlink now!!!"
#   rm -f ~/.gitconfig
#   ln -s -f ~/.dotfiles/.gitconfig  ~/.gitconfig
# fi

# cat << 'EOF' >> /etc/zsh/zshrc
# source /usr/share/autojump/autojump.sh
# source ~/.dotfiles/.alias.sh
# source ~/.dotfiles/.exports.sh
# source ~/.dotfiles/.functions.sh
# EOF
# cat /etc/zsh/zshrc


if [ -f ~/.hushlogin ]; then
  echo "~/.hushlogin file has been touched"
else 
  touch ~/.hushlogin
fi

ssh -T git@gitlab.com

